package com.example.fp.cgc.controller

import com.example.fp.cgc.entity.UserFollow
import com.example.fp.cgc.mapper.UserFollowMapper
import com.example.fp.cgc.util.DataUtil
import com.example.fp.cgc.websocket.WebSocketServer
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Lazy
import org.springframework.web.bind.annotation.*
import springfox.documentation.swagger2.annotations.EnableSwagger2

/**
 * @since 2020-02-25
 * @author 蔡
 */
@RestController
@RequestMapping
@EnableSwagger2
open class FollowController {
    @Autowired
    @Lazy
    lateinit var userFollowMapper: UserFollowMapper
    /**
     * 更改关注状态
     * @since 2020-02-26
     * @author 蔡
     */
    @GetMapping(value = ["/changeFollow/{uid}&{oid}"])
    open fun changeFollow(
            @PathVariable("uid") uid: Int,
            @PathVariable("oid") oid: Int
    ) {
        val follow = userFollowMapper.isFollow(uid,oid)
        if (follow.isEmpty()){
            userFollowMapper.insert(UserFollow(uid,oid))
        }else{
            userFollowMapper.deleteFollow(uid,oid)
        }
        WebSocketServer.onlineUserMap[uid]?.sendMessage(DataUtil.getFollowList(uid))
        WebSocketServer.onlineUserMap[uid]?.sendMessage(DataUtil.getUserOnlineState())
    }
}